home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / x / xfig.lha / src / x11 / u_print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-26  |  4.9 KB  |  168 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1985 by Supoj Sutanthavibul
  4.  *
  5.  * "Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both the copyright
  8.  * notice and this permission notice appear in supporting documentation. 
  9.  * No representations are made about the suitability of this software for 
  10.  * any purpose.  It is provided "as is" without express or implied warranty."
  11.  */
  12.  
  13. #include "fig.h"
  14. #include "mode.h"
  15. #include "resources.h"
  16.  
  17. /*
  18.  * Beware!  The string returned by this function is static and is
  19.  * reused the next time the function is called!
  20.  */
  21.  
  22. char *shell_protect_string(string)
  23.     char       *string;
  24. {
  25.     static char *buf = 0;
  26.     static int buflen = 0;
  27.     int len = 2 * strlen(string) + 1;
  28.     char *cp, *cp2;
  29.  
  30.     if (! buf) {
  31.     buf = XtMalloc(len);
  32.     buflen = len;
  33.     }
  34.     else if (buflen < len) {
  35.     buf = XtRealloc(buf, len);
  36.     buflen = len;
  37.     }
  38.  
  39.     for (cp = string, cp2 = buf; *cp; cp++) {
  40.     *cp2++ = '\\';
  41.     *cp2++ = *cp;
  42.     }
  43.  
  44.     *cp2 = '\0';
  45.  
  46.     return(buf);
  47. }
  48.  
  49. print_to_printer(printer, mag, flushleft, params)
  50.     char        printer[];
  51.     Boolean        flushleft;
  52.     float        mag;
  53.     char        params[];
  54. {
  55.     char        prcmd[2*PATH_MAX+200], translator[60];
  56.     char        syspr[2*PATH_MAX+200];
  57.     char        tmpfile[32];
  58.  
  59.     sprintf(tmpfile, "%s/%s%06d", TMPDIR, "xfig-print", getpid());
  60.     warnexist = False;
  61.     if (write_file(tmpfile))
  62.     return;
  63.  
  64.     sprintf(translator, "fig2dev -Lps %s -P -m %f %s",
  65.         flushleft ? "" : "-c" ,
  66.         mag,
  67.         print_landscape ? "-l xxx" : " ");
  68.  
  69.  
  70.     if (emptyname(printer)) {    /* send to default printer */
  71. #if defined(SYSV) || defined(SVR4)
  72.     sprintf(syspr, "lp %s -oPS", params);
  73. #else
  74.     sprintf(syspr, "lpr %s -J %s", params, shell_protect_string(cur_filename));
  75. #endif
  76.     put_msg("Printing figure on default printer in %s mode ...     ",
  77.         print_landscape ? "LANDSCAPE" : "PORTRAIT");
  78.     } else {
  79. #if defined(SYSV) || defined(SVR4)
  80.     sprintf(syspr, "lp %s -d%s -oPS", params, printer);
  81. #else
  82.     sprintf(syspr, "lpr %s -J %s -P%s", params, shell_protect_string(cur_filename),
  83.         printer);
  84. #endif
  85.     put_msg("Printing figure on printer %s in %s mode ...     ",
  86.         printer, print_landscape ? "LANDSCAPE" : "PORTRAIT");
  87.     }
  88.     app_flush();        /* make sure message gets displayed */
  89.  
  90.     /* make up the whole translate/print command */
  91.     sprintf(prcmd, "%s %s | %s", translator, tmpfile, syspr);
  92.     if (system(prcmd) != 0)
  93.     file_msg("Error during PRINT (check standard error output)");
  94.     else {
  95.     if (emptyname(printer))
  96.         put_msg("Printing figure on printer %s in %s mode ... done",
  97.             printer, print_landscape ? "LANDSCAPE" : "PORTRAIT");
  98.     else
  99.         put_msg("Printing figure on printer %s in %s mode ... done",
  100.             printer, print_landscape ? "LANDSCAPE" : "PORTRAIT");
  101.     }
  102.     unlink(tmpfile);
  103. }
  104.  
  105. print_to_file(file, lang, mag, flushleft)
  106.     char       *file, *lang;
  107.     float        mag;
  108.     Boolean        flushleft;
  109. {
  110.     char        prcmd[2*PATH_MAX+200];
  111.     char        tmp_name[PATH_MAX];
  112.     char        tmp_fig_file[32];
  113.     char       *outfile;
  114.     int            tlen, status;
  115.  
  116.     /* if file exists, ask if ok */
  117.     if (!ok_to_write(file, "EXPORT"))
  118.     return (1);
  119.  
  120.     sprintf(tmp_fig_file, "%s/%s%06d", TMPDIR, "xfig-fig", getpid());
  121.     /* write the fig objects to a temporary file */
  122.     warnexist = False;
  123.     if (write_file(tmp_fig_file))
  124.     return (1);
  125.     outfile = shell_protect_string(file);
  126.  
  127.     put_msg("Exporting figure to file \"%s\" in %s mode ...     ",
  128.         file, print_landscape ? "LANDSCAPE" : "PORTRAIT");
  129.     app_flush();        /* make sure message gets displayed */
  130.  
  131.     if (!strcmp(lang, "ps"))
  132.     sprintf(prcmd, "fig2dev -Lps %s -P -m %f %s %s %s", flushleft ? "" : "-c" ,
  133.         mag, print_landscape ? "-l xxx" : " ", tmp_fig_file,
  134.         outfile);
  135.     else if (!strcmp(lang, "eps"))
  136.     sprintf(prcmd, "fig2dev -Lps -m %f %s %s %s",
  137.         mag, print_landscape ? "-l xxx" : " ", tmp_fig_file,
  138.         outfile);
  139.     else if (!strcmp(lang, "ibmgl"))
  140.     sprintf(prcmd, "fig2dev -Libmgl -m %f %s %s %s",
  141.         mag, print_landscape ? " " : "-P", tmp_fig_file,
  142.         outfile);
  143.     else if (!strcmp(lang, "pstex_t")) {
  144.     /* make it automatically input the postscript part */
  145.     strcpy(tmp_name, file);
  146.     tlen = strlen(tmp_name);
  147.     if (tlen > 2) {
  148.         if (tmp_name[tlen-1] == 't' && tmp_name[tlen-2] == '_')
  149.         tmp_name[tlen-2] = '\0';
  150.         else
  151.         tmp_name[0] = '\0';
  152.     } else
  153.         tmp_name[0] = '\0';
  154.     sprintf(prcmd, "fig2dev -Lpstex_t -p %s -m %f %s %s",
  155.         tmp_name, mag, tmp_fig_file, outfile);
  156.     } else
  157.     sprintf(prcmd, "fig2dev -L%s -m %f %s %s", lang,
  158.         mag, tmp_fig_file, outfile);
  159.     if (system(prcmd) != 0)
  160.     file_msg("Error during EXPORT (check standard error output)");
  161.     else
  162.     put_msg("Exporting figure to file \"%s\" in %s mode ... done",
  163.         file, print_landscape ? "LANDSCAPE" : "PORTRAIT");
  164.  
  165.     unlink(tmp_fig_file);
  166.     return (0);
  167. }
  168.